package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.util.Log
import android.webkit.WebChromeClient
import android.webkit.WebViewClient
import com.hyphenate.chat.EMClient
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.customview.JiePanPayDialog
import hlhj.fhp.supreme.javabean.ArticleBean
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.toast
import hlhj.fhp.supreme.utils.JsonCallBack
import kotlinx.android.synthetic.main.aty_article.*

/**
 * Created by Never Fear   on 2018\7\18 0018.
Never More....
 */
class ArticleAty :BaseAty() {
    private var tittle=""
    override fun getContentId(): Int {
        return R.layout.aty_article
    }


    override fun initView() {
        btGoin.isClickable=false
        initWeb()
        dispatchData()
    }

    private fun dispatchData() {
        OkGo.post<ArticleBean>(Urls.BaseUrl+Urls.ARTICLE_DETAIL)
                .params("id",intent.getStringExtra("id"))
                .execute(object :JsonCallBack<ArticleBean>(this@ArticleAty){
                    override fun onSuccess(response: Response<ArticleBean>?) {
                        val articleBean = response?.body()
                        if (articleBean?.code==1){
                            btGoin.isClickable=true
                            tittle=articleBean.data.title
                            var prompt = articleBean.data.content
                            prompt=prompt.replace("<img", "<img style='max-width:100%;height:auto;'")
                            webView.loadDataWithBaseURL(null, prompt,"text/html", "utf-8", null)
                        }else if (articleBean?.code==403){
                            JiePanPayDialog(this@ArticleAty,"111").show()
                            finish()
                        }
                        else{

                            this@ArticleAty.toast(this@ArticleAty,articleBean?.msg!!)
                        }
                    }
                })
    }

    private fun initWeb() {
        webView.webViewClient=object :WebViewClient(){

        }
        webView.webChromeClient=object :WebChromeClient(){

        }
    }

    override fun initListener() {

        btExit.setOnClickListener {
            finish()
        }
        btGoin.setOnClickListener {
            OkGo.post<BaseBean>(Urls.BaseUrl+ Urls.IS_JIEPAN)
                    .execute(object : JsonCallBack<BaseBean>(this@ArticleAty){
                        override fun onSuccess(response: Response<BaseBean>?) {
                            val bean = response?.body()
                            if (bean?.code==1){
                                Thread(Runnable {
                                    try {
                                        Log.e("fhppp","走到哪了？0")

                                        EMClient.getInstance().groupManager().joinGroup(intent.getStringExtra("rid"))

                                    }catch (e:Exception){
                                            Log.e("报错",e.toString())
                                    }
                                }).start()
                                Thread(Runnable {
                                    runOnUiThread {
                                        loadingDialog.show()
                                    }
                                    Log.e("fhppp","走到哪了？1")
                                    val group = EMClient.getInstance().groupManager().getGroupFromServer(intent.getStringExtra("rid"))
                                    Log.e("fhppp","走到哪了？2 Group为空？${group==null}")
                                    if (group!=null){
                                        loadingDialog.dismiss()
                                        val i = Intent(this@ArticleAty, JiePanDetailAty::class.java)
                                        i.putExtra("roomId",intent.getStringExtra("rid"))
                                        i.putExtra("name",tittle)
                                        i.putExtra("owner",group.owner)
                                        Log.e("fhppp","第二次传入的id${intent.getStringExtra("rid")}")
                                        startActivity(i)
                                    }
                                }).start()
                            }else{
                                JiePanPayDialog(this@ArticleAty,"20").show()

                            }
                        }
                    })
            //            startActivity(Intent(this,JiePanTextDetailAty::class.java))

        }
    }
}